﻿using System;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCService.TMS.LearningTrain;
using BCCommon.TMS.Driver;
using BCTMSRESTful.Configs;
using BCDto.TMS.LearnTrain.VehicleTrainingScheduleRecord;
using BCDto.TMS.LearnTrain.VehicleTrainingScheduleRecord.TrainingScheduleRecordRequest;
using BCDto.TMS.LearnTrain.PendingLearningVehicle;
using System.Collections.Generic;
using BCDto.TMS.LearnTrain;
using BCDto.TMS.LearnTrain.LearnTrainStatistics;
using BCService.TMS.LearningTrain.DriverLearningTrainRecord;
using BCDto.TMS.LearnTrain.DriverLearningTrainRecord;
using BCDto.TMS.LearnTrain.PendingLearningVehicle.PendingLearningVehicleRequestDto;

namespace BCTMSRESTful.Controllers.Management.TMS.LearnTrain
{
    /// <summary>
    /// 骑手培训控制器
    /// </summary>
    [ApiExplorerSettings(GroupName = "TMSAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class LearnTrainController : ManagementBaseController
    {
        private readonly IPendingLearningVehicleService pendingLearningVehicleService;
        private readonly IVehicleTrainingScheduleRecordService vehicleTrainingScheduleRecordService;
        private readonly IDriverLearningTrainRecordService driverLearningTrainRecordService;
        public LearnTrainController(IPendingLearningVehicleService pendingLearningVehicleService,
            IVehicleTrainingScheduleRecordService vehicleTrainingScheduleRecordService,
            IDriverLearningTrainRecordService driverLearningTrainRecordService)
        {
            this.pendingLearningVehicleService = pendingLearningVehicleService;
            this.vehicleTrainingScheduleRecordService = vehicleTrainingScheduleRecordService;
            this.driverLearningTrainRecordService = driverLearningTrainRecordService;
        }

        /// <summary>
        /// 获取两个月的培训考试通过人数统计
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        /// <response code="200">获取两个月的培训考试通过人数统计。</response>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(LearnTrainAnalysisDto), 200)]
        [Obsolete]
        public IActionResult TrainSeheduleMonthAnalysis(
            [FromQuery(Name = "date")] DateTime? date)
        {
            var result = this.pendingLearningVehicleService.GetTrainSeheduleMonthAnalysis(this.Admin, date);
            return this.Ok(result);
        }

        #region  培训人数和考试人数合计
        /// <summary>
        /// 司机培训人数统计
        /// </summary>
        /// <param name="datetime">时间</param>
        /// <param name="trainingSiteConfigId">培训点配置id</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(LearnTrainDto), 200)]
        public IActionResult LearnTrainCount(
            [FromQuery(Name = "datetime")] DateTime? datetime,
            [FromQuery(Name = "training_site_config_id")] long? trainingSiteConfigId)
        {
            if (Admin.AgentRecord == null)
            {
                throw new ArgumentException(" 用户认证失败,错误操作 ");
            }
            var result = this.pendingLearningVehicleService.GetLearningDriverCount(Admin.AgentRecord.AgentRecordId, datetime, trainingSiteConfigId);
            return this.Ok(result);
        }

        /// <summary>
        /// 考试人数
        /// </summary>
        /// <param name="datetime">日期</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(ExamineDriverCountDto), 200)]
        [Obsolete]
        public IActionResult DriverExamineCount([FromQuery(Name = "datetime")] DateTime? datetime)
        {
            var result = this.pendingLearningVehicleService.QueryFirstExamDriverCount(this.Admin, datetime);
            return this.Ok(result);
        }

        /// <summary>
        /// 累计获取培训人数
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(TrainCountDto), 200)]
        public IActionResult QueryTrainCount()
        {
            return this.Ok(this.pendingLearningVehicleService.QueryTrainCountDetailList(this.Admin));
        }
        #endregion

        #region 待培训司机列表
        /// <summary>
        /// 获取待培训司机列表
        /// </summary>
        /// <param name="driverName">司机名称</param>
        /// <param name="vehicleNo">车辆编号</param>
        /// <param name="driverRoleType">司机角色</param>
        /// <param name="isVehicleTrainingScheduleRecord">是否安排培训</param>
        /// <param name="notifyStartDate">通知开始日期</param>
        /// <param name="notifyEndDate">通知结束日期</param>
        /// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        /// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<PendingLearningVehicleViewDto>), 200)]
        public IActionResult Query([FromQuery(Name = "driver_name")] string driverName,
            [FromQuery(Name = "vehicle_no")] string vehicleNo,
            [FromQuery(Name = "driver_role_type")] DriverRoleType? driverRoleType,
            [FromQuery(Name = "is_vehicle_training_schedule_record")] bool? isVehicleTrainingScheduleRecord,
            [FromQuery(Name = "notify_start_date")] DateTime? notifyStartDate,
            [FromQuery(Name = "notify_end_date")] DateTime? notifyEndDate,
            [FromQuery(Name = "page_index")] int pageIndex = 1,
            [FromQuery(Name = "page_size")] int pageSize = 10)
        {
            var result = this.pendingLearningVehicleService.Query(this.Admin,driverName, vehicleNo, driverRoleType, isVehicleTrainingScheduleRecord,
                notifyStartDate, notifyEndDate, pageIndex, pageSize);
            return this.Ok(result);
        }

        /// <summary>
        /// 获取培训安排选择的培训场次
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<VehicleTrainingScheduleRecordDto>), 200)]
        public IActionResult QueryTwicePendingRecord([FromQuery(Name = "date")] DateTime? Date)
        {
            var result = this.vehicleTrainingScheduleRecordService.GetTwicePendingRecord(this.Admin, Date);
            return this.Ok(result);
        }


        /// <summary>
        /// 添加培训安排
        /// </summary>
        /// <param name="requestDto"></param>
        /// <returns></returns>
        [HttpPost("[action]")]
        [Log("添加车辆培训计划")]
        [ProducesResponseType(typeof(VehicleTrainingScheduleRecordDto), 200)]
        public IActionResult AddVehicleTrainingScheduleRecord([FromBody] VehicleTrainingScheduleRecordRequestDto requestDto)
        {
            var result = this.vehicleTrainingScheduleRecordService.Add(this.Admin, requestDto);
            return this.Ok(result);
        }

        /// <summary>
        ///  追加培训安排
        /// </summary>
        /// <param name="putDto">数据</param>
        /// <param name="vehicleTrainingScheduleRecordId">培训期数编号</param>
        /// <returns></returns>
        [HttpPut("[action]/{vehicle_training_schedule_record_id}")]
        [Log("追加车辆培训计划参与车辆")]
        [ProducesResponseType(typeof(VehicleTrainingScheduleRecordDto), 200)]
        public IActionResult TwiceAddVehicleTrainingScheduleRecord([FromRoute(Name = "vehicle_training_schedule_record_id")] long vehicleTrainingScheduleRecordId,
            [FromBody] VehicleTrainingScheduleRecordPutDto putDto)
        {
            var result = this.vehicleTrainingScheduleRecordService.TwiceAddVehicleTrainingScheduleRecord(this.Admin, vehicleTrainingScheduleRecordId, putDto);
            return this.Ok(result);
        }
        #endregion

        #region 培训记录
        /// <summary>
        /// 培训记录
        /// </summary>
        /// <param name="trainDate">培训日期</param>
        /// <param name="trainingSiteConfigId">培训点</param>
        /// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        /// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<VehicleTrainingScheduleRecordViewDto>), 200)]
        public IActionResult TrainRecord([FromQuery(Name = "train_date")] DateTime? trainDate,
            [FromQuery(Name = "training_site_config_id")] long? trainingSiteConfigId,
            [FromQuery(Name = "page_index")] int pageIndex = 1,
            [FromQuery(Name = "page_size")] int pageSize = 10)
        {
            var result = this.vehicleTrainingScheduleRecordService.GetTrainRecord(this.Admin, trainingSiteConfigId, trainDate, pageIndex, pageSize);
            return this.Ok(result);
        }

        /// <summary>
        /// 获取车辆培训安排详情
        /// </summary>
        /// <param name="vehicleTrainingScheduleRecordId">车辆培训安排记录id</param>
        /// <param name="phone">电话</param>
        /// <returns></returns>
        [HttpGet("[action]/{vehicle_training_schedule_record_id}")]
        [ProducesResponseType(typeof(IEnumerable<VehicleTrainingScheduleDetailDto>), 200)]
        public IActionResult QueryVehicleTrainingScheduleDetail(
            [FromRoute(Name = "vehicle_training_schedule_record_id")] long vehicleTrainingScheduleRecordId,
            [FromQuery(Name = "phone")] string phone)
        {
            var result = this.pendingLearningVehicleService.GetVehicleTrainingScheduleDetail(this.Admin, vehicleTrainingScheduleRecordId, phone);
            return this.Ok(result);
        }

        /// <summary>
        /// 获取 车辆培训安排统计
        /// </summary>
        /// <param name="vehicleTrainingScheduleRecordId">培训安排id</param>
        /// <returns></returns>
        [HttpGet("[action]/{vehicle_training_schedule_record_id}")]
        [ProducesResponseType(typeof(VehicleTrainingScheduleStatisticsDto), 200)]
        public IActionResult GetTrainingScheduleStatistics([FromRoute(Name = "vehicle_training_schedule_record_id")] long vehicleTrainingScheduleRecordId)
        {
            return this.Ok(this.pendingLearningVehicleService.GetTrainingScheduleStatistics(vehicleTrainingScheduleRecordId));
        }
        #endregion

        #region 缺考以及允许考试操作
        /// <summary>
        ///  缺考
        /// </summary>
        /// <param name="vehicleTrainingScheduleRecordId">考试安排合计表id</param>
        /// <param name="pendingLearningVehicleId">待培训id</param>
        /// <returns></returns>
        [Log("设置缺考")]
        [HttpPut("[action]/{vehicle_training_schedule_record_id}/{pending_learning_vehicle_id}")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult MissExtension([FromRoute(Name = "vehicle_training_schedule_record_id")] long vehicleTrainingScheduleRecordId,
            [FromRoute(Name = "pending_learning_vehicle_id")] long pendingLearningVehicleId)
        {
            return this.Ok(this.pendingLearningVehicleService.MissExtension(this.Admin, vehicleTrainingScheduleRecordId, pendingLearningVehicleId));
        }


        /// <summary>
        /// 允许考试
        /// </summary>
        /// <param name="vehicleTrainingScheduleRecordId">培训安排记录id</param>
        /// <param name="putDto">已培训人员id</param>
        /// <returns></returns>
        [Log("允许已培训的人员参加考试")]
        [HttpPut("[action]/{vehicle_training_schedule_record_id}")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult AllowedExamination([FromRoute(Name = "vehicle_training_schedule_record_id")] long vehicleTrainingScheduleRecordId,
            [FromBody] VehicleTrainingScheduleRecordPutDto putDto)
        {
            return this.Ok(this.pendingLearningVehicleService.AllowJoinExam(this.Admin, vehicleTrainingScheduleRecordId, putDto));
        }

        /// <summary>
        /// 考试通过
        /// </summary>
        /// <param name="vehicleTrainingScheduleRecordId">培训安排记录id</param>
        /// <param name="requestDto">待培训id</param>
        /// <returns></returns>
        [Log("考试通过")]
        [HttpPut("[action]/{vehicle_training_schedule_record_id}")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult LearnTrainPassed([FromRoute(Name = "vehicle_training_schedule_record_id")] long vehicleTrainingScheduleRecordId,
            [FromBody]PendingLearningVehicleIdRequestDto requestDto)
        {
            if (this.Admin.AgentRecord==null)
            {
                throw new ArgumentException(" 用户认证失败,错误操作 ");
            }

            var result =
                this.driverLearningTrainRecordService.LearnTrainPassed(this.Admin, vehicleTrainingScheduleRecordId,
                    requestDto);
            return this.Ok(result);
        }

        #endregion

        //#region 未签约司机
        ///// <summary>
        ///// 获取未签约司机
        ///// </summary>
        ///// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        ///// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        ///// <param name="PartnerId">合伙人id</param>
        ///// <returns></returns>
        //[HttpGet("[action]")]
        //[ProducesResponseType(typeof(PageableList<UnSignContractDto>), 200)]
        //public IActionResult UnSignContract([FromQuery(Name = "partner_id")] long? PartnerId,
        //    int pageIndex = 1,
        //    int pageSize = 10)
        //{
        //    return this.Ok(learnTrainService.QueryUnSignContractDto(PartnerId, pageIndex, pageSize));
        //}

        ///// <summary>
        ///// 获取未签约司机沟通记录
        ///// </summary>
        ///// <param name="DriverId"></param>
        ///// <returns></returns>
        //[HttpGet("[action]")]
        //[ProducesResponseType(typeof(IEnumerable<UnSignContractCommunicationDto>), 200)]
        //public IActionResult QueryUnSignContractCommunication([FromQuery(Name = "driver_id")]long DriverId)
        //{
        //    return this.Ok(learnTrainService.QueryUnSignContractCommunicationDtos(DriverId));
        //}

        ///// <summary>
        ///// 添加未签约司机沟通记录
        ///// </summary>
        ///// <param name="DriverId">司机编号</param>
        ///// <param name="MarkDto">沟通记录</param>
        ///// <returns></returns>
        //[Log("添加未签约司机沟通记录")]
        //[HttpPost("[action]/{driver_id}")]
        //[ProducesResponseType(typeof(UnSignContractCommunicationDto), 200)]
        //public IActionResult QueryInsertUnSignContractCommunication([FromRoute(Name = "driver_id")]long DriverId, [FromBody]MarkDto MarkDto)
        //{
        //    var result = learnTrainService.QueryInsertUnSignContractCommunicationDtos(this.Admin,DriverId, MarkDto.Mark);
        //    return Created(string.Format("api/v1/sim/learntrain/{0}", result), result);
        //}
        //#endregion

        #region  培训历史记录

        /// <summary>
        /// 获取培训历史记录
        /// </summary>
        /// <param name="vehicleNo">车牌号</param>
        /// <param name="driverName">司机名称</param>
        /// <param name="driverRoleType">司机角色</param>
        /// <param name="phone">手机号</param>
        /// <param name="trainingSiteConfigId">培训点</param>
        /// <param name="trainingStartDate">培训开始日期</param>
        /// <param name="trainingEndDate">培训结束日期</param>
        /// <param name="learnTrainSignEndDate">培训签到结束日期</param>
        /// <param name="learnTrainPassedStartDate">培训通过开始日期</param>
        /// <param name="learnTrainPassedEndDate">培训通用结束日期</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="learnTrainSignStartDate">培训签到开始日期</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<DriverLearningTrainRecordViewDto>), 200)]
        public IActionResult QueryLearnTrainHistoryRecord([FromQuery(Name = "vehicle_no")] string vehicleNo,
            [FromQuery(Name = "driver_name")] string driverName,
            [FromQuery(Name = "driver_role_type")] DriverRoleType? driverRoleType,
            [FromQuery(Name = "phone")] string phone,
            [FromQuery(Name = "training_site_config_id")] long? trainingSiteConfigId,
            [FromQuery(Name = "training_start_date")] DateTime? trainingStartDate,
            [FromQuery(Name = "training_end_date")] DateTime? trainingEndDate,
            [FromQuery(Name = "learn_train_passed_start_date")] DateTime? learnTrainPassedStartDate,
            [FromQuery(Name = "learn_train_passed_end_date")] DateTime? learnTrainPassedEndDate,
            [FromQuery(Name = "learn_train_sign_start_date")] DateTime? learnTrainSignStartDate,
            [FromQuery(Name = "learn_train_sign_end_date")] DateTime? learnTrainSignEndDate,
            [FromQuery(Name = "page_index")] int pageIndex = 1,
            [FromQuery(Name = "page_size")] int pageSize = 10)
        {
            var result = driverLearningTrainRecordService.GetListAsync(this.Admin, driverName, driverRoleType, vehicleNo,
                    phone, trainingSiteConfigId, trainingStartDate, trainingEndDate, learnTrainPassedStartDate, learnTrainPassedEndDate,
                    learnTrainSignStartDate, learnTrainSignEndDate,
                    pageIndex, pageSize);
            return this.Ok(result);
        }
        #endregion


        #region 进入考场
        /// <summary>
        /// 考场数据统计
        /// </summary>
        /// <param name="vehicleTrainingScheduleRecordId">培训期数编号</param>
        /// <returns></returns>
        [HttpGet("[action]/{vehicle_training_schedule_record_id}")]
        [ProducesResponseType(typeof(ExaminationStatisticsDto), 200)]
        public IActionResult ExaminationStatistics([FromRoute(Name = "vehicle_training_schedule_record_id")] long vehicleTrainingScheduleRecordId)
        {
            return this.Ok(this.pendingLearningVehicleService.GetExaminationStatistics(vehicleTrainingScheduleRecordId));
        }

        /// <summary>
        /// 获取考试司机信息
        /// </summary>
        /// <param name="vehicleTrainingScheduleRecordId">培训期数编号</param>
        /// <param name="isLearnTrainPassed">是否考试通过</param>
        /// <returns></returns>
        [HttpGet("[action]/{vehicle_training_schedule_record_id}/{is_learn_train_passed}")]
        [ProducesResponseType(typeof(IEnumerable<LearnTrainExaminationDriverDto>), 200)]
        public IActionResult ExaminationDriverInfo([FromRoute(Name = "vehicle_training_schedule_record_id")] long vehicleTrainingScheduleRecordId,
            [FromRoute(Name = "is_learn_train_passed")] bool isLearnTrainPassed)
        {
            return this.Ok(this.pendingLearningVehicleService.GetExaminationDriverInfo(vehicleTrainingScheduleRecordId, isLearnTrainPassed));
        }

        /// <summary>
        /// 考试中
        /// </summary>
        /// <param name="vehicleTrainingScheduleRecordId">培训期数编号</param>
        /// <returns></returns>
        [HttpGet("[action]/{vehicle_training_schedule_record_id}")]
        [ProducesResponseType(typeof(IEnumerable<LearnTrainExaminationDriverDto>), 200)]
        public IActionResult ExaminationHaving([FromRoute(Name = "vehicle_training_schedule_record_id")] long vehicleTrainingScheduleRecordId)
        {
            return this.Ok(this.pendingLearningVehicleService.GetExaminationHaving(vehicleTrainingScheduleRecordId));
        }
        #endregion




    }
}
